// import java.util.ArrayList;
// import java.util.Random;
import java.util.Scanner;
/*********************************************************************
 * Homework 6:  Implement a binary search tree for doing a
 *              dictionary lookup.
 *
 * @author Duncan Buell
**/
public class Driver
{
/*********************************************************************
 * main method
**/
  public static void main (String[] args)
  {
    final String classLabel = "Main: ";
    String testString;
    Scanner inFile = null;
    Scanner testFile = null;
    Record rec = null;
    BTNode v = null;
    BST theTree;

    inFile = FileUtils.ScannerOpen("zin.fullpaper");
    FileUtils.setLogFile("zlog");

    /*********************************************************************
     * First we build the tree of the dictionary.
    **/
    theTree = new BST();
    while(inFile.hasNext())
    {
      rec = new Record();
      rec = rec.readRecord(inFile);
      v = new BTNode(rec,null,null,null);
      if(v.getElement().isAlpha())
      {
        FileUtils.logFile.printf("%s is alpha, insert '%s'%n",
                  classLabel,v.getElement().getElement());
        FileUtils.logFile.flush();
        theTree.insert(v);
      }
      else
      {
        FileUtils.logFile.printf("%s NOT ALPHA, skip '%s'%n",
                  classLabel,v.getElement().getElement());
        FileUtils.logFile.flush();
      }
    } // while(inFile.hasNext())

    /*********************************************************************
     * We have finished reading the input data, so we echo the list.
    **/
    FileUtils.logFile.printf("%s The inorder tree of all %d items%n%s%n",
              classLabel,
              theTree.getSize(),theTree.toStringInorder(theTree.getRoot()));
    
    FileUtils.logFile.printf("%s The average height is %d/%d=%f%n",
              classLabel,
              theTree.getTotalHeight(),theTree.getNodeCount(),
             (double) theTree.getTotalHeight()/theTree.getNodeCount());
             
    theTree.writeHeightHisto();
    FileUtils.logFile.flush();

/*
    FileUtils.logFile.printf("%s The preorder tree of all %d items%n%s%n",
              classLabel,
              theTree.getSize(),theTree.toStringPreorder(theTree.getRoot()));
    FileUtils.logFile.flush();

    FileUtils.logFile.printf("%s The postorder tree of all %d items%n%s%n",
              classLabel,
              theTree.getSize(),theTree.toStringPostorder(theTree.getRoot()));
*/
    FileUtils.logFile.flush();
    FileUtils.closeFile(inFile);

    /*********************************************************************
     * Now we run the lookup on the test file.
    **/
    testFile = FileUtils.ScannerOpen("ztest");
    testString = "";
    while(testFile.hasNext())
    {
      testString = testFile.next();
      FileUtils.logFile.printf("%s The test string is '%s'%n",
                classLabel,testString);
      FileUtils.logFile.flush();
      if(theTree.contains(testString))
      {
        FileUtils.logFile.printf("%s The test string '%s' is in the tree%n%n",
                  classLabel,testString);
        FileUtils.logFile.flush();
      }
      else
      {
        FileUtils.logFile.printf("%s The test string '%s' is NOT in the tree%n%n",
                  classLabel,testString);
        FileUtils.logFile.flush();
      }
    } // while(testFile.hasNext())

    /*********************************************************************
     * Close up, go home.
    **/
    FileUtils.logFile.printf("%s no more data, terminate%n",classLabel);
    FileUtils.logFile.flush();
    
    FileUtils.closeLogFile();
  }
} // public class Main





